package org.gitos.cloud.server.mq.logic;


import org.gitos.cloud.domain.comm.Constant;
import org.gitos.cloud.domain.device.Router;
import org.gitos.cloud.domain.mq.MqMsg;
import org.gitos.cloud.domain.server.Led;
import org.gitos.cloud.domain.tcp.Bucket;
import org.gitos.cloud.domain.tcp.DeviceId;
import org.gitos.cloud.server.mq.PushManager;
import org.gitos.cloud.server.service.PushToAppService;
import org.gitos.cloud.server.service.RelationService;
import org.gitos.cloud.util.HexUtil;

/**
 * Created by szc on 2015/12/4.
 */
public class DisconnectHandler implements Runnable {
    private PushManager pushManager;
    private PushToAppService pushToAppService;
    private String channleId;
    private String nodeId;
    private MqMsg msg;
    private Router router;

    public DisconnectHandler(MqMsg msg, Router router,PushToAppService pushToAppService) {
        this.msg = msg;
        this.router=router;
        this.pushToAppService=pushToAppService;
    }

    public MqMsg getMsg() {
        return msg;
    }

    public void setMsg(MqMsg msg) {
        this.msg = msg;
    }

    public PushManager getPushManager() {
        return pushManager;
    }

    public void setPushManager(PushManager pushManager) {
        this.pushManager = pushManager;
    }

    public String getChannleId() {
        return channleId;
    }

    public void setChannleId(String channleId) {
        this.channleId = channleId;
    }

    public String getNodeId() {
        return nodeId;
    }

    public void setNodeId(String nodeId) {
        this.nodeId = nodeId;
    }

    public Router getRouter() {
        return router;
    }

    public void setRouter(Router router) {
        this.router = router;
    }

    @Override
    public void run() {
        MqMsg resultMsg = new MqMsg();
        resultMsg.setChannelId(channleId);
        resultMsg.setNodeId(nodeId);
        Bucket bucket = new Bucket();
        bucket.setCmd(Constant.CMD_DOWN_CLOSE);
        DeviceId deviceId = msg.getBucket().getDeviceId();
        bucket.setDeviceId(deviceId);
        resultMsg.setBucket(bucket);
        router.deleteDevice(deviceId);
        pushToAppService.push(HexUtil.encodeHexStr(deviceId.getValue()),PushToAppService.CLOSE);
        pushManager.sendMsg(resultMsg.getNodeId(), resultMsg);
    }
}
